# -*-coding:utf-8-*-
# Copyright (c) 2020 DJI, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License in the file LICENSE.txt or at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import time
import robomaster
from robomaster import robot
import cv2
import os
from datetime import datetime


if __name__ == '__main__':
    if not os.path.exists('debug_video'):
        os.mkdir('debug_video')
    # 录像保存
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    now = datetime.now()
    file_name = 'debug_video/' + 'debug_video' + now.strftime('%Y%m%d%H%M%S') + '.avi'
    video_out = cv2.VideoWriter(file_name, fourcc, 20.0, (640, 360))
    ep_robot = robot.Robot()
    ep_robot.initialize(conn_type="sta")

    ep_camera = ep_robot.camera
    ep_arm = ep_robot.robotic_arm
    ep_chassis = ep_robot.chassis
    ep_arm.moveto(x=89, y=33).wait_for_completed()
    # 显示100帧图传。
    ep_camera.start_video_stream(display=False)
    # ep_chassis.move(2,0,0)
    for i in range(0, 500):
        img = ep_camera.read_cv2_image()
        img = cv2.resize(img,(640,360))
        cv2.imshow("Robot", img)
        cv2.waitKey(1)
        video_out.write(img)
    cv2.destroyAllWindows()
    out.release()
    ep_camera.stop_video_stream()

    ep_robot.close()